package com.lyz.moil.web.action;

import com.lyz.moil.core.orm.PageData;
import com.lyz.moil.core.utils.WebUtils;
import com.lyz.moil.web.entity.Device;
import com.lyz.moil.web.service.DeviceService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/moil/device")
public class DeviceAction {
    @Resource
    private DeviceService deviceService;

    @RequestMapping("/list")
    public ModelMap list(HttpServletRequest request) {
        PageData<Device> pageData = new PageData<Device>();
        // 给pageData设置参数
        WebUtils.setPageDataParameter(request, pageData);
        pageData = deviceService.find(pageData);

        return new ModelMap(pageData);
    }

    @RequestMapping(value = "/new")
    public String addForm(Model model) {
        return "/moil/device/input";
    }

    @RequestMapping(value = "/edit/{id}")
    public String editForm(@PathVariable("id") Integer id, Model model) {
        Device device = deviceService.find(id);
        model.addAttribute(device);
        return "/moil/device/input";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Device device, HttpServletRequest request) {
        if (device.isNew()) {
            deviceService.save(device);
        } else {
            deviceService.update(device);
        }
        return "redirect:/moil/device/list";// 重定向
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        deviceService.delete(id);
        return "redirect:/moil/device/list";// 重定向
    }

    @RequestMapping(value = "/visible/{id}")
    public String visible(@PathVariable("id") Integer id) {
        deviceService.visible(id);
        return "redirect:/moil/device/list";// 重定向
    }

    @RequestMapping(value = "/unVisible/{id}")
    public String unVisible(@PathVariable("id") Integer id) {
        deviceService.unVisible(id);
        return "redirect:/moil/device/list";// 重定向
    }

    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    @ResponseBody
    public Device reg(HttpServletRequest request, Device device) throws Exception {
        deviceService.save(device);
        return device;
    }

//    @RequestMapping(value = "/reg", method = RequestMethod.POST)
//    public ResponseData reg(HttpServletRequest request, ResponseData response) throws Exception {
//        ResponseData responseData = new ResponseData();
//        responseData.setStatus(Response.Status.OK.getStatusCode());
//        Device device = (Device) response.getBody();
//
//        deviceService.save(device);
//
//        JSONObject r = new JSONObject();
//        r.put("id", device.getUuid());
//        r.put("aesKey", Aes.AES_KEY);
//        responseData.setBody(r);
//        return responseData;
//    }

    @InitBinder
    public void InitBinder(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
    }

}
